//
//  HMNavigationViewController.swift
//  微博
//
//  Created by 郭鉴增 on 2016/12/2.
//  Copyright © 2016年 郭鉴增. All rights reserved.
//

import UIKit

class HMNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置代理手势
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    //重写push方法,监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        print(viewControllers.count)
        
        //隐藏底部tabBar
        if viewControllers.count > 0 {
            //隐藏tabBar
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
        if viewControllers.count > 1{
            if viewControllers.count == 2 {
                //获取导航栏里面的第一个子视图控制器的标题
                let title = viewControllers.first!.title!
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
                
            }else{
                //统一标题改成返回
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
            viewController.title = "当前现实的是第\(viewControllers.count)级控制器"
        }
    }
    
    @objc private func popAction(){
        popViewController(animated: true)
    }
}
//MARK: - 手势代理
extension HMNavigationViewController:UIGestureRecognizerDelegate{
    //是否处理这次点击边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1{
            //如果是跟视图控制器不让处理边缘手势
            return false
        }
        
        return true
    }
    
    
}
